11 class Solution
12 {
13 public:
14 ListNode* swapPairs(ListNode* head)
15 {
16 if(head == nullptr || head->next == nullptr) return head;
17
18 ListNode* newHead = new ListNode(0);
19 newHead->next = head;
20
21 ListNode* prev = newHead, *cur = prev->next, *next = cur->next, *nnext =
22 while(cur && next)
23 {
24 // 交换结点
25 prev->next = next;
26 next->next = cur;
27 cur->next = nnext;
28
29 // 修改指针
30 prev = cur; // 注意顺序
31 cur = nnext;
32 if(cur) next = cur->next;
33 if(next) nnext = next->next;
34 }
35 cur = newHead->next;
36 delete newHead;
37 return cur;
38 }
39 };